﻿
using ZFY.Blog.Api.Model.Dto.ArticleTypeDto;
using ZFY.Blog.Api.Model.Model;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ZFY.AspNetCore;

namespace ZFY.Blog.Api.AutoMapper.ArticleTypeResolver
{
    public class ArticleTypeImageResolver : IValueResolver<CreateArticleTypeInput, ArticleType, string>
    {
        public string Resolve(CreateArticleTypeInput source, ArticleType destination, string destMember, ResolutionContext context)
        {
            var dir = StaticInfo.WebRootPath + "/ArticleTypeImage/";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var file = source.Image;
            var url = dir + Guid.NewGuid() + file.FileName.Substring(file.FileName.LastIndexOf("."));
            var stream = file.OpenReadStream();
            var bytes = new byte[file.Length];
            stream.Read(bytes, 0, (int)file.Length);
            File.WriteAllBytes(url, bytes);
            return url.Substring(url.IndexOf("/ArticleTypeImage/"));
        }
    }
}
